<template>
  <!-- 礼品卡管理 -->
  <div class="gift_card">
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange" :isHaveAdd="true"
        @handleSizeChange="handleSizeChange" @sendUserSearch="receive_search" :searchData="userSearchData" :isHaveExport="false" @toAdd="add_data">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="loading" header-row-class-name="head_color" tooltip-effect="dark"
            style="width: 99.8%" @selection-change="handleSelectionChange" :default-sort="{ prop: 'sort' }">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="排序" min-width="80">
            <template slot-scope="scope">{{ scope.row.sort }}</template>
          </el-table-column>

          <el-table-column label="名称" min-width="80">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="图片" min-width="80" align="center">
            <template slot-scope="scope">
              <div class="user_table">
                <img :src="$fnc.getImgUrl(scope.row.piclink)" style="width:80px;height:80px;border-radius:50%;" alt />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="价格" min-width="80">
            <template slot-scope="scope">{{
              scope.row.price | keepTwoNum
            }}</template>
          </el-table-column>

          <el-table-column label="成本价" min-width="80">
            <template slot-scope="scope">{{
              scope.row.cost | keepTwoNum
            }}</template>
          </el-table-column>

          <el-table-column label="面值" min-width="80">
            <template slot-scope="scope">{{
              scope.row.money | keepTwoNum
            }}</template>
          </el-table-column>

          <el-table-column label="说明" min-width="80">
            <template slot-scope="scope">
              <p v-html="scope.row.content"></p>
            </template>
          </el-table-column>

          <el-table-column prop="name" label="是否开启" align="center" min-width="80">
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.show" active-color="#1aad19" inactive-color="#ffffff" active-text="开启"
                  inactive-text="关闭" active-value="1" inactive-value="0" @change="getShow($event, scope.row)"></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="160">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="editbtn(scope.row.id)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deletebtn(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template> -->
    </tableComponents>
  </div>
</template>

<script>
import uploadpic from "@/components/page/pic/up_pic";
import { giftSearchData } from "@/assets/js/searchData/searchData"; // 引入搜索的数据
export default {
  data () {
    return {
      userSearchData: giftSearchData,
      list: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      multipleSelection: [],
      loading: false
    };
  },
  components: {
    uploadpic
  },
  methods: {
    receive_search (obj) {
      //搜索
      console.log(obj);
      this.searchAll = obj;
      this.getList(obj);
    },
    getList (data) {
      //获取列表
      this.loading = true;
      var params = data || {};
      this.$api.getShops.getGiftCard(params).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    editbtn (id) {
      //编辑
      this.$store.commit("addbreadcrumbs", {
        title: "礼品卡编辑",
        url: "/index/shop/gift_card_edit?id=" + id
      });

      this.$router.push("/index/shop/gift_card_edit?id=" + id);
    },
    handleSelectionChange (val) {
      //多选删除
      this.multipleSelection = val;
    },
    deletebtn (id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops
            .delGiftCard({
              id: id
            })
            .then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.getList(this.searchAll);
              } else {
                this.$swal("删除失败！", res.data.result, "error");
                this.getList(this.searchAll);
              }
            });
        }
      });
    },
    // deleteLises() {
    //   //批量删除
    //   var str = "";
    //   if (this.multipleSelection.length <= 0) {
    //     this.$message.error("未选择批量删除");
    //   } else {
    //     for (var i in this.multipleSelection) {
    //       str += this.multipleSelection[i].id + "@";
    //     }
    //     str = str.slice(0, -1);
    //     this.$swal({
    //       title: "您确定要删除这些信息吗",
    //       text: "删除后将无法恢复，请谨慎操作！",
    //       type: "warning",
    //       showCancelButton: true,
    //       confirmButtonColor: "#DD6B55",
    //       confirmButtonText: "是的，我要删除",
    //       cancelButtonText: "容我三思",
    //     }).then((willDelete) => {
    //       if (willDelete.value) {
    //         var data = {
    //           id_str: str
    //         };
    //         this.$api.getShops.delGiftCard(data).then((res) => {
    //           if (res.data.code == 200) {
    //             this.$swal("删除成功！", "您已经删除了这些信息。", "success");
    //             this.getNovel(this.searchAll);
    //           } else {
    //             this.$swal("删除失败！", "出错啦！", "error");
    //           }
    //         });
    //       }
    //     });
    //   }
    // },
    add_data () {
      //添加
      this.$store.commit("addbreadcrumbs", {
        title: "礼品卡添加",
        url: "/index/shop/gift_card_edit"
      });
      this.$router.push("/index/shop/gift_card_edit");
    },
    onSuccess (pic, index) {
      //图片上传
    },
    handleSizeChange (val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getList(params);
      this.pageSize = val;
    },
    handleCurrentChange (val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getList(params);
    },
    getShow (index, row) {
      //是否开启
      row.show = index;
      this.$api.getShops.editGiftCard(row).then(res => {
        if (res.data.code == 200) {
          return;
        }
      });
    }
  },
  created () {
    this.getList();
  }
};
</script>

<style></style>
